package com.uhylab.uhuvinetest.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.UHApplication;
import com.uhylab.uhuvinetest.base.UHBase;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.utils.VolleySingleton;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称：Uhylab
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/5/31 14:12
 * 修改人：xyb
 * 修改时间：16/5/31 14:12
 * 修改备注：
 */
public class UHUserInformationActivity extends UHBaseActivity {


    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.user_icon)
    ImageView mUserIcon;
    @BindView(R.id.name_user)
    TextView mNameUser;
    @BindView(R.id.user_tel)
    TextView mUserTel;
    @BindView(R.id.tel_user)
    TextView mTelUser;
    @BindView(R.id.user_password)
    TextView mUserPassword;
    @BindView(R.id.change_password)
    TextView mChangePassword;


    private String name;
    private String tel;
    private String mSex;
    private String mDate;
    private String mWeight;
    private String mHeight;
    private VolleySingleton volleySingleton;

    @Override
    public int bindLayout() {
        return R.layout.activity_userinformation;
    }

    @Override
    public void initView(View view) {
        ButterKnife.bind(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        Intent intent = getIntent();
        if (intent != null) {
            name = intent.getStringExtra("name");
            tel = intent.getStringExtra("tel");
            mSex = intent.getStringExtra("sex");
            mDate = intent.getStringExtra("date");
            mWeight = intent.getStringExtra("weight");
            mHeight = intent.getStringExtra("height");
            mNameUser.setText(name);
            mTelUser.setText(tel);
        }
        volleySingleton = VolleySingleton.getVolleySingleton(UHApplication.gainContext());


    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }


    @OnClick({R.id.back, R.id.name_user, R.id.tel_user, R.id.change_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.name_user:
                showExchangeNameDialog();
                break;
            case R.id.tel_user:
                break;
            case R.id.change_password:
                startActivity(new Intent(this, UHBackPasswordActivity.class).setAction("1"));
                break;
        }
    }


    private void showExchangeNameDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater layoutInflater = getLayoutInflater();
        View inflate = layoutInflater.inflate(R.layout.smssdk_set_appkey_dialog, null, false);
        builder.setView(inflate);
        final AlertDialog alertDialog = builder.create();
        final EditText etAppKey = (EditText) inflate.findViewById(R.id.et_appkey);
        etAppKey.setHint("请输入用户名");
        View.OnClickListener ocl = new View.OnClickListener() {
            public void onClick(View v) {
                if (v.getId() == R.id.btn_dialog_ok) {
                    if (etAppKey.getText().toString().trim() != null) {
                        name = etAppKey.getText().toString();
                        mNameUser.setText(name);
                        request();
                        alertDialog.dismiss();
                    } else {
                        Toast.makeText(UHUserInformationActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    alertDialog.dismiss();

                }
            }
        };
        inflate.findViewById(R.id.btn_dialog_ok).setOnClickListener(ocl);
        inflate.findViewById(R.id.btn_dialog_cancel).setOnClickListener(ocl);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }


//    public void request() {
//        StringRequest exchangeRe = new StringRequest(Request.Method.POST, UHBase.editDetails, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        }) {
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                //在这里设置需要post的参数
//                Map<String, String> params = new HashMap<String, String>();
//                UserDataEntity userDataEntity = new UserDataEntity();
//                if (tel != null) {
//                    params.put("telephone", tel);
//                    userDataEntity.setTelephone(tel);
//                }
//                if (mSex != null) {
//                    params.put("sex", mSex);
//                    userDataEntity.setSex(mSex);
//                }
//                if (mDate != null) {
//                    params.put("date", mDate);
//                    userDataEntity.setBirthday(mDate);
//                }
//                if (mWeight != null) {
//                    params.put("weight", mWeight);
//                    userDataEntity.setWeight(mWeight);
//                }
//                if (mHeight != null) {
//                    params.put("height", mHeight);
//                    userDataEntity.setHeight(mHeight);
//                }
//                if (name != null) {
//                    params.put("name", name);
//                    userDataEntity.setName(name);
//                }
//                return params;
//            }
//        };
//        volleySingleton.addQueue(exchangeRe, "exchangeRe");
//    }


    public void request() {
        String user_token = SharedPreUtils.getString(this, "user_token", null);
        try {
            String tel = SharedPreUtils.getString(this, "user_tel", "");
            JSONObject jsonObject = new JSONObject();
            JSONObject userJson = new JSONObject();
            if (tel != null) {
                userJson.put("phone", tel);
            }
            if (mSex != null) {
                if (mSex.equals("F")) {
                    userJson.put("sex", 0);
                } else {
                    userJson.put("sex", 1);
                }

            }
            if (mDate != null) {
                userJson.put("birthday", mDate);
            }
            if (mWeight != null) {
                userJson.put("weight", mWeight);
            }
            if (mHeight != null) {
                userJson.put("height", mHeight);
            }
            if (name != null) {
                userJson.put("name", name);
            }
            userJson.put("bloodtype", null);
            jsonObject.put("isUpdate", 1);
            jsonObject.put("user", userJson);

            JsonObjectRequest exchangeRe = new JsonObjectRequest(Request.Method.PUT, UHBase.newUserInfoEdit + "token=" + user_token, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Accept", "application/json");
                    headers.put("Content-Type", "application/json; charset=UTF-8");
                    return headers;
                }

            };
            volleySingleton.addQueue(exchangeRe, "exchangeRe");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
